package elearning.discuss.dao;

import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import elearning.course.entity.Course;
import elearning.discuss.entity.Answer;
import elearning.discuss.entity.IfAgree;
import elearning.discuss.entity.Question;
import elearning.discuss.entity.Reply;
import elearning.record.entity.Browse;
import elearning.user.entity.User;
import elearning.video.entity.Video;


public class DiscussDao extends HibernateDaoSupport{

	public Question findByQueId(Integer queId) {
		
		
		
		return this.getHibernateTemplate().get(Question.class,queId);
		
	}

	public List<Answer> findAnsByQueId(Integer id) {
		String hql = "from Answer where queId=?";
		@SuppressWarnings("unchecked")
		List<Answer> list = this.getHibernateTemplate().find(hql,id);
		if(list.size()>0&&list!=null)
			return list;
		return null;
	}

	public List<Reply> findReplyByAnsId(Integer ansId) {
		String hql="from Reply where ansId=?";
		@SuppressWarnings("unchecked")
		List<Reply> list = this.getHibernateTemplate().find(hql,ansId);
		if(list.size()>0&&list!=null)
			return list;
		return null;
	}

	public Boolean save(Answer answer) {
		try{
		this.getHibernateTemplate().save(answer);
		}catch(Exception e){
			return false;
		}
		return true;
		
	}

	public Answer findByAnsId(Integer ansId) {
		Answer answer = this.getHibernateTemplate().get(Answer.class,ansId);
		
		return answer;
	}

	public void update(Answer answer) {
		this.getHibernateTemplate().update(answer);
	}

	public IfAgree findAgreeFlag(int i, Integer ansId) {
		String hql = "from IfAgree where userId=? and ansId=?";
		@SuppressWarnings("unchecked")
		List<IfAgree> list = this.getHibernateTemplate().find(hql,i,ansId);
		if(list.size()>0&&list!=null){
			return list.get(0);
		}
		return null;
	}

	public void updateIfAgree(IfAgree ifAgree) {
		this.getHibernateTemplate().update(ifAgree);
	}

	public void saveifAgree(IfAgree ifAgree) {
		this.getHibernateTemplate().save(ifAgree);
	}

	public void saveReply(Reply reply) {
		this.getHibernateTemplate().save(reply);
	}

	public List<Question> findQuestionByCourseId(Integer courseId) {
	
		String hql="from Question where videoId in (select videoId from Video where courseId = "+courseId+")";
		@SuppressWarnings("unchecked")
		List<Question> list = this.getHibernateTemplate().find(hql);
		if(list.size()>0&&list!=null){
			return list;
		}
		return null;
	}

	public User findbyUserId(Integer userId) {
		
		return this.getHibernateTemplate().get(User.class,userId);
	}

	public int findCountByQueId(Integer queId) {
		String hql = "select count(*) from Answer where queId = ?";
		@SuppressWarnings("unchecked")
		List<Long> list = this.getHibernateTemplate().find(hql,queId);
		if(list!=null && list.size()>0)
		{
			return list.get(0).intValue();
		}		
		return 0;	
	}

	public String findNewAnswerByQueId(Integer queId) {
		String hql = "select ansContent from Answer where queId = ? order by ansId desc";
		@SuppressWarnings("unchecked")
		List<String> list = this.getHibernateTemplate().find(hql,queId);
		if(list.size()>0&&list!=null){
			return list.get(0);
		}
		return null;
	}

	public List<Question> findQueContentByUid(int uid) {
		String hql = "from Question where userId = ?";
		@SuppressWarnings("unchecked")
		List<Question> list = this.getHibernateTemplate().find(hql,uid);
		if(list.size()>0&&list!=null){
			return list;
		}
		return null;
	}

	public String findCourseNameByVideoId(Integer videoId) {
		
		Video video = this.getHibernateTemplate().get(Video.class,videoId);
		if(video!=null){
			return video.getCourse().getCourseName();
		}
		return null;
	}


	public String[][] findAnswerByqueId(Integer queId) {
		String hql = "from Answer where queId =  ? ";
		String[][] answer = new String[2][4];
		@SuppressWarnings("unchecked")
		List<Answer> list = this.getHibernateTemplate().find(hql,queId);
		if(list!=null&&list.size()>0){
			answer[0][0] = list.get(0).getAnsContent();
			answer[0][1] = list.get(0).getAnsTime();
			answer[0][2] = list.get(0).getUserId()+"";
			answer[0][3] = list.get(0).getAgreeNumber()+"";
			
			if(list.get(1)!=null){
				answer[1][0] = list.get(1).getAnsContent();
				answer[1][1] = list.get(1).getAnsTime();
				answer[1][2] = list.get(1).getUserId()+"";
				answer[1][3] = list.get(1).getAgreeNumber()+"";
			}
			return answer;
		}
		return null;
	}

	public List<Answer> findAnswerByUid(int uid) {
		String hql = "from Answer where userId = ?";
		@SuppressWarnings("unchecked")
		List<Answer> list = this.getHibernateTemplate().find(hql,uid);
		if(list.size()>0&&list!=null){
			return list;
		}
		return null;
	}

	public String findCourseNameByAnswerId(Integer queId) {
		Question question = this.getHibernateTemplate().get(Question.class,queId);
		Video video = this.getHibernateTemplate().get(Video.class,question.getVideoId());		
		if(video!=null){
			return video.getCourse().getCourseName();
		}
		return null;
	}

	public String findQuestionNameByQueId(Integer queId) {
		Question question = this.getHibernateTemplate().get(Question.class,queId);		
		if(question!=null){
			return question.getQueContent();
		}
		return null;
	}

	public void save(Question question) {
		this.getHibernateTemplate().save(question);
	}

	public Question findByContent(String content) {
		String hql = "from Question where queContent = "+"'"+content+"'";
		@SuppressWarnings("unchecked")
		List<Question> list = this.getHibernateTemplate().find(hql);
		if(list.size()>0&&list!=null){
			return list.get(0);
		}
		return null;
	}

	public Integer findUserImgByUid(int uid) {
		String hql  = "select userImg from User where userId = ?";
		@SuppressWarnings("unchecked")
		List<Integer> list =this.getHibernateTemplate().find(hql,uid);
		if(list.size()>0&&list!=null){
			return list.get(0);
		}
		return null;
	}

	public String findUserNickNameByUid(int uid) {
		String hql  = "select nickName from User where userId = ?";
		@SuppressWarnings("unchecked")
		List<String> list =this.getHibernateTemplate().find(hql,uid);
		if(list.size()>0&&list!=null){
			return list.get(0);
		}
		return null;
	}

	public Integer findUserIdByQueId(Integer queId) {
		String hql  = "select userId from Question where queId = ?";
		@SuppressWarnings("unchecked")
		List<Integer> list =this.getHibernateTemplate().find(hql,queId);
		if(list.size()>0&&list!=null){
			return list.get(0);
		}
		return null;
	}

	public List<Question> findQueContentByQueId(Integer videoId) {
		String hql = "from Question where videoId = ? order by queId desc";
		@SuppressWarnings("unchecked")
		List<Question> list = this.getHibernateTemplate().find(hql,videoId);
		if(list.size()>0&&list!=null){
			return list;
		}
		return null;
	}

	public Browse findBrowse(Integer uid, Integer videoId) {
		String hql="from Browse where userId = ? and videoId = ?";
		List<Browse> list = this.getHibernateTemplate().find(hql,uid,videoId);
		if(list.size()>0&&list!=null){
			return list.get(0);
		}
		return null;
	}

	public Integer findCourseIdbyVideoId(Integer videoId) {
		String hql ="from Video where videoId ="+videoId;
		List<Video> list = this.getHibernateTemplate().find(hql);
		if(list!=null&&list.size()>0){
			return list.get(0).getCourse().getCourseId();
		}
		return null;
	}

	public Integer findCsidbyCourseId(Integer courseId) {
		Course course = this.getHibernateTemplate().get(Course.class,courseId);
		return course.getCategorySecond().getCsid();
	}

	public List<Course> findCourse(Integer csid) {
		String hql = "from Course where csid = ? order by courseWatched desc";
		List<Course> list = this.getHibernateTemplate().find(hql,csid);
		if(list!=null&&list.size()>0){
			return list;
		}
		return null;
	}

	

}
